<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$tabs=array(
	array('op'=>'list','title'=>'推广主等级管理'),
	array('op'=>'edit','title'=>'添加/编辑等级'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='level asc';
	$total='';
	$return['list']=$this->getall('level',$params,array(),'',$orderby);
	
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、平台发放推广比例：是指一级下线，消费产生的佣金！
		</br>2、<red>说明：填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放1%的佣金；</red>
		','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重（数字越大，权重越大）','type'=>'string'),
			array('type'=>'string','name'=>'fee_per1','title'=>'推广人头激活费'),
			array('type'=>'string','name'=>'fee_per','title'=>'推广佣金(购买指定商品)'),
			array('type'=>'string','name'=>'jindoufee','title'=>'知识币奖励'),
		),'action'=>array(
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0))
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['conditions']=$item['conditions']?json_decode($item['conditions'],true):array();
	}else{
		$item=array(
			'title'=>'',
			'conditions'=>array('order_price'=>'0')
		);
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string','help'=>'填写一百以内的数字，数字越大表示等级越高！不同等级的等级权重不得相等！'),
				array('type'=>'string','name'=>'fee_per1','title'=>'推广人头激活费','help'=>'说明：填写数字表示实际费用'),
			array('type'=>'string','name'=>'fee_per','title'=>'推广费用','help'=>'说明：填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金；'),
			array('name'=>'buy_creditfrom','title'=>'下单奖励来源','type'=>'radio','options'=>'buy_creditfrom'),
			array('type'=>'string','name'=>'jindoufee','title'=>'知识币奖励','help'=>'说明：填写数字表示实际可增送的知识币可用于问题的回答与询问；'),
		))),
	);
	$return['item']=$item;
	$return['level']=$this->pdo_getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['buy_creditfrom']=$this->buy_creditfrom;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		//'area_type'=>intval($postdata['area_type']),
		'level'=>intval($postdata['level']),
		'conditions'=>$postdata['conditions']?ijson_encode($postdata['conditions']):'',
		'fee_per'=>trim($postdata['fee_per']),
		'fee_per1'=>trim($postdata['fee_per1']),
		'buy_creditfrom'=>$postdata['buy_creditfrom'],
		'isyidong'=>intval($postdata['isyidong']),
		'jindoufee'=>trim($postdata['jindoufee']),
	);
	if($data['level']<=0 || $data['level']>100){
		exi('请填写正确的等级权重值，1-100','error');
	}
	$id=intval($postdata['id']);
	if($id){
		$islevel=$this->getcolumn('level',array('uniacid'=>$_SESSION['uniacid'],'level'=>$postdata['level'],'id <>'=>$id),'id');
		if($islevel){
			exi('该权重已设置过，不能重复！','error');
		}
		$this->update('level',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$islevel=$this->getcolumn('level',array('uniacid'=>$_SESSION['uniacid'],'level'=>$postdata['level']),'id');
		if($islevel){
			exi('该权重已设置过，不能重复！','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$this->insert('level',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('level',array('id'=>$id));	
	}
	if(!$id){
		exi('默认等级不能删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('level',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>